import javax.lang.model.type.ArrayType;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ForkJoinPool;

public class Test01 {
    public  static int maxPoints(int n, int[] points , int[][] dependencies, int k){
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        //填充图
        for (int[] dep : dependencies) {
            graph.get(dep[1]).add(dep[0]);
        }
        int[] inDegree = new int[n];
        for(int[] dep:dependencies){
            inDegree[dep[0]]++;
        }
        Queue<Integer> que = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            if(inDegree[i] == 0){
                que.offer(i);
            }
        }
        List<Integer> order = new ArrayList<>();
        while (!que.isEmpty()){
            int cur = que.poll();
            order.add(cur);
            for(int next : graph.get(cur)){
                if(--inDegree[next] == 0){
                    que.offer(next);
                }
            }
        }
        if(order.size() < n){
            return -1;
        }

        int[][] dp = new int[n+1][k+1];
        for (int i = 1; i <= n ; i++) {
            int s = order.get(i-1);
            for (int j = 1; j <= k; j++) {
                dp[i][j] = Math.max(dp[i][j],dp[i-1][j]);
                if(j >= 1){
                    dp[i][j] = Math.max(dp[i][j],dp[i-1][j-1]+points[s]);
                }
            }
        }
        return dp[n][k];
    }
}
